package com.example.mall.dao;

import com.example.mall.model.Join;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class JoinDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Join join) {
        String sql = "insert into join (assembleID, accountID) values (?, ?)";
        jdbcTemplate.update(sql, join.getAssembleid(), join.getAccountid());
    }

    public void delete(Integer assembleID, Integer accountID) {
        String sql = "delete from join where assembleID = ? and accountID = ?";
        jdbcTemplate.update(sql, assembleID, accountID);
    }

    public List<Join> getByAid(Integer accountID) {
        String sql = "select * from join where accountID = ? lock in share mode";
        RowMapper<Join> rowMapper = new BeanPropertyRowMapper<>(Join.class);
        return jdbcTemplate.query(sql, rowMapper, accountID);
    }

}
